package com.haoma.controller;

import com.haoma.dto.CategoryDto;
import com.haoma.dto.PhotoDto;
import com.haoma.entity.PhotoCategory;
import com.haoma.result.Result;
import com.haoma.service.PhotoCategoryService;
import com.haoma.service.PhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Description: 相册控制层
 */
@RestController
@RequestMapping("/admin/photo")
public class PhotoController {


    @Autowired
    private PhotoCategoryService photoCategoryService;

    @Autowired
    private PhotoService photoService;

    /**
     * 获取所有的分类
     *
     * @return
     */
    @GetMapping("/category")
    public Result getPhotoCategory() {
        List<PhotoCategory> photoCategoryList = photoCategoryService.getPhotoCategory();
        return Result.success(photoCategoryList);
    }

    /**
     * 分类的封面上传
     *
     * @param multipartFile
     * @return
     */
    @PostMapping("/fileUpload")
    public Result fileUpload(@RequestParam("file") MultipartFile multipartFile) {
        String fileUrl = photoCategoryService.fileUpload(multipartFile);
        return Result.success(fileUrl);
    }

    /**
     * 相册图片上传
     *
     * @param multipartFile
     * @return
     */
    @PostMapping("/photoUpload/{id}")
    public Result photoUpload(@PathVariable Integer id, @RequestParam("file") MultipartFile multipartFile) {
        photoService.photoUpload(id, multipartFile);
        return Result.success();
    }

    /**
     * 新增分类
     *
     * @param categoryDto
     * @return
     */
    @PostMapping("/category/save")
    public Result saveCategory(@RequestBody @Validated CategoryDto categoryDto) {
        photoCategoryService.saveCategory(categoryDto);
        return Result.success();
    }

    /**
     * 根据分类id删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping("/category/del/{id}")
    public Result delCategory(@PathVariable Integer id) {
        photoCategoryService.delCategory(id);
        return Result.success();
    }

    /**
     * 修改相册分类
     *
     * @param categoryDto
     * @return
     */
    @PutMapping("/category/update/{id}")
    public Result updateCategory(@PathVariable Integer id, @RequestBody @Validated CategoryDto categoryDto) {
        photoCategoryService.updateCategory(id, categoryDto);
        return Result.success();
    }

    /**
     * 根据id回显相册分类数据
     *
     * @param id
     * @return
     */
    @GetMapping("/category/{id}")
    public Result getCategory(@PathVariable Integer id) {
        PhotoCategory photoCategory = photoCategoryService.getCategory(id);
        return Result.success(photoCategory);
    }

    /**
     * 根据分类id获取分类下的照片
     *
     * @param state
     * @return
     */
    @GetMapping("/photo/{id}/{state}")
    public Result getPhoto(@PathVariable Integer state, @PathVariable(required = false) Integer id) {
        List<String> photoList = photoService.getPhotoById(state, id);
        return Result.success(photoList);
    }

    /**
     * 删除分类下的图片
     *
     * @param photoDto
     * @return
     */
    @DeleteMapping("/photo")
    public Result delPhoto(@RequestBody PhotoDto photoDto) {
        photoService.delPhoto(photoDto);
        return Result.success();
    }

    /**
     * 隐藏图片
     * 约定1就是隐藏2显示3回收站
     *
     * @param photoDto
     * @return
     */
    @PutMapping("/conceal/{state}")
    public Result concealPhoto(@PathVariable Integer state, @RequestBody @Validated PhotoDto photoDto) {
        photoService.concealPhoto(state, photoDto);
        return Result.success();
    }

    /**
     * 获取所有的视频
     *
     * @return
     */
    @GetMapping("/video/{state}")
    public Result getVideoList(@PathVariable Integer state) {
        List<String> videoUrlList = photoService.getVideoList(state);
        return Result.success(videoUrlList);
    }


}
